Zepto(function ($) {
  $(".nav-header").on("click", ".back", function (e) {
    window.history.go(-1);
  });
  $(".comfire-goods").click(function () {
    window.location.href="/usercenter/order/returnreason.html"
  });
  var zOrderInfoId = sessionStorage.getItem("zOrderInfoId");
  var specificationId = "";
  window.onload = function () {
    getOrderDetails();
  }
  function getOrderDetails() {
    $.ajax({
      type: 'GET',
      url: getUrl + '/wxmall/order/selectOrderInfo',
      data: {
        "zOrderInfoId": zOrderInfoId
      },
      dataType: 'json',
      timeout: 15000,
      headers: headers("order.selectOrderInfo"),
      success: function(data){
        initView(data)
      },
      error: function(xhr, type){
      }
    });
  }
  function initView(data) {
    console.log(data);
    if(!data.success){
      return
    }
    var data1 = data.data;
    $(".order-name")[0].innerHTML= data1.receiver_name;
    $(".address-phone")[0].innerHTML= data1.contact_mobile;
    $(".address")[0].innerHTML= data1.region + data1.detailed_address;
    if(data1.post_money != ""){
      $("#post_money")[0].innerHTML= "(含运费:￥"+data1.post_money+")";
    }
    $(".red-goods")[0].innerHTML = "￥" + data1.order_total_money;
    $("#goodsList").html("");
    var goodslist = data1.orderGoodsInfo;
    if(goodslist.length > 0 ){
      var html = "";
      var allnumber = "0";
      for(var j = 0; j< goodslist.length ; j++){
        var picUrl = "";
        if(goodslist[j].picUrl != null&& goodslist[j].picUrl != undefined){
          picUrl = imgUrl + goodslist[j].picUrl;
        }
        var goods_name = "";
        if(goodslist[j].goods_name != null&& goodslist[j].goods_name != undefined){
          goods_name = goodslist[j].goods_name;
        }
        var specification = "";
        if(goodslist[j].specification != null&& goodslist[j].specification != undefined){
          specification = goodslist[j].specification;
        }

        if(goodslist[j].specificationId != null&& goodslist[j].specificationId != undefined){
          specificationId = goodslist[j].specificationId;
        }
        var price = "";
        if(goodslist[j].price != null&& goodslist[j].price != undefined){
          price ="￥" + goodslist[j].price;
        }
        var discount_price = "";
        if(goodslist[j].discount_price != null&& goodslist[j].discount_price != undefined){
          discount_price ="￥" + goodslist[j].discount_price;
        }
        var number = "";
        if(goodslist[j].number != null&& goodslist[j].number != undefined){
          number ="X" + goodslist[j].number;
          allnumber = parseInt(allnumber) + parseInt(goodslist[j].number);
        }
        html += '<div class="goods-list">\n' +
            '                            <div class="left"><img src="'+picUrl+'" /></div>\n' +
            '                            <div class="center">\n' +
            '                                <div class="center-content">\n' +
            '                                    <div class="goods-desc">'+goods_name+'</div>\n' +
            '                                    <div class="goods-specification">'+specification+'</div>\n' +
            '                                </div>\n' +
            '                            </div>\n' +
            '                            <div class="right">\n' +
            '                                <div class="goods-prise"><span class="z-price">'+price+'</span><span class="s-price">'+discount_price+'</span></div>\n' +
            '                                <div class="goods-num">'+number+'</div>\n' +
            '                            </div>\n' +
            '                        </div>'+
            '                        <div class="handle-way1">\n' +
            '                            <div class="comfire-goods">退款</div>\n' +
            '                            <div class="add-cart">加入购物车</div>\n' +
            '                        </div>';
      }
      $("#goodsList").append(html);
      $("#allnumber")[0].innerHTML= allnumber;
    }
    $("#order_no")[0].innerHTML =data1.zorder_no;
    var addTime =data1.addTime
    $("#addDate")[0].innerHTML = addTime.substring(0,10);
    $("#addTime")[0].innerHTML =addTime.substring(11,16);
  }


  /**
   * 加入购物车
   *
   */
  $(".glo").on("click",".comfire-tocart",function () {
    event.stopPropagation();
    var btnArray = ['否', '是'];
    mui.confirm('再次购买订单所有商品吗？', '', btnArray, function(e) {
      if (e.index == 1) {
        var data1 = {
          "specificationId":specificationId,
          "num":"1"
        }
        tocart(data1);
      }
    });
  });
  /**
   * 加入购物车
   *
   */
  function tocart(data1) {
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/shopcarts/insert',
      data: data1,
      dataType: 'json',
      timeout: 15000,
      // contentType:'application/json',
      headers: headers("shopcarts.insert"),
      context: $('body'),
      success: function(data){
        tocartbefore(data);
      },
      error: function(xhr, type, data){
        console.log(xhr,type,data);
      }
    });
  }
  function tocartbefore(data) {
    if(data.success){
      $(".model").css("display","none");
      mui.toast("已加入购物车");
    }else {
      $(".model").css("display","none");
      mui.toast(data.msg);
    }
  }
});